package com.biz.service.MService.user;


import com.biz.model.Hmodel.base.TBaseUser;
import com.biz.model.Pmodel.base.PbaseUser;
import com.biz.model.Pmodel.base.PmyApplyActivity;
import com.biz.model.Pmodel.base.PsysDynamic;
import com.biz.model.Pmodel.weixin.UserInfo;
import com.biz.service.HService.base.BaseServiceImpl;
import com.biz.service.weixin.WXUtilService;
import com.biz.utils.ConfigUtil;
import com.framework.dao.hdao.BaseDaoI;
import com.framework.dao.mdao.DaoSupport;
import com.framework.util.weixin.WechatAccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*************************************************************************
 * create by lzq
 *
 * 文件名称 ：UserServiceI.java 描述说明 ：
 *
 * 版权信息 : Copyright (c) 2015 扬州北斗软件有限公司
 **************************************************************************/

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<TBaseUser> implements UserServiceI {

    @Resource(name = "wxUtilService")
    private WXUtilService wxUtilService;

    @Resource(name = "daoSupport")
    private DaoSupport dao;


    @Autowired
    private BaseDaoI<TBaseUser> tBaseUserDao;


    @Override
    public PbaseUser queryUserInfoByOpenId(String openId) {
        PbaseUser pbaseUser = null;
        try {
            pbaseUser = (PbaseUser) dao.findForObject("UserDao.getUserByOpenId", openId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pbaseUser;
    }


    @Override
    public PbaseUser  saveUserInfoByOpenIdAndReturn(String openId) {
        PbaseUser pbaseUserAlready = null;
        synchronized (this) {
            //判断是否注册
            try {
                pbaseUserAlready =  (PbaseUser) dao.findForObject("UserDao.getUserByOpenId", openId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(pbaseUserAlready != null && pbaseUserAlready.getId() != null )
            {
                return pbaseUserAlready;
            }
            //保存数据
            TBaseUser tBaseUser = new TBaseUser();
            tBaseUser.setOpenId(openId);
            try {
                // 获取用户微信头像url
                String access_token = wxUtilService.getAccessToken();
                UserInfo userInfo = WechatAccessToken.getUserInfo(access_token, openId);
                tBaseUser.setCover(userInfo.getHeadimgurl());
                tBaseUser.setPersonName(userInfo.getNickname());
                tBaseUser.setCountry(userInfo.getCountry());
                tBaseUser.setProvince(userInfo.getProvince());
                tBaseUser.setCity(userInfo.getCity());
                tBaseUser.setSex((short)userInfo.getSex());
            } catch (Exception e) {
                e.printStackTrace();
            }
            tBaseUserDao.save(tBaseUser);

            //返回数据
            PbaseUser pbaseUser = new PbaseUser();
            pbaseUser.setOpenId(openId);
            pbaseUser.setId(tBaseUser.getId());
            return pbaseUser;
        }
    }


    @Override
    public List<PmyApplyActivity> getUserApplyListByUserId(String userId)throws Exception{


        return (List<PmyApplyActivity>)dao.findForList("UserDao.getUserApplyListByUserId",userId);
    }

    @Override
    public Map<String,Object> getCountByUserId(String userId)throws Exception{

        return (Map<String,Object>)dao.findForObject("UserDao.getCountByUserId",userId);
    }


    @Override
    public Map<String, String> queryRegInfoByphone(String phone) throws Exception {
        return (Map<String, String>) dao.findForObject(
                "UserDao.queryRegInfoByphone", phone);
    }

    @Override
    public Map<String, String> updateRegister(String userId, String userName,String phone, HttpSession session) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        map.put("userId", userId);
        map.put("phone", phone);
        map.put("userName",userName);
        int count=0;
        count=(Integer) dao.update("UserDao.updateUserOld", map);
        if (count > 0) {
            session.setAttribute(ConfigUtil.get("SESSION_USER_PHONE"),phone);
            map.put("result", "success");
        } else {
            map.put("result", "fail");
            map.put("msg", "保存出错,请稍后再试");
        }
        return map;
    }

    @Override
    public List<Map<String, String>> getInteralTotalListByUserId(String userId)throws Exception{

        return (List<Map<String, String>>) dao.findForList(
                "UserDao.getInteralTotalListByUserId", userId);
    }

    @Override
    public List<PsysDynamic> getPsysDynamicList() throws Exception{

        return (List<PsysDynamic>)dao.findForList("UserDao.getPsysDynamicList",null);
    }

    @Override
    public PsysDynamic getPsysDynamicById(String detailId)throws Exception{

        return (PsysDynamic)dao.findForObject("UserDao.getPsysDynamicById",detailId);
    }
}
